package config

import (
	"fmt"
	"os"

	"gopkg.in/yaml.v2"
)

type Config struct {
	MysqlS       []*MySQLConf       `yaml:"mysql_s"`
	HttpAddr     string             `yaml:"http_addr"`
	RpcAddr      string             `yaml:"rpc_addr"`
	PCC          *PublicCloudConf   `yaml:"public_cloud_sync"`
	IndexModules []*IndexModuleConf `yaml:"index_modules"`
}

type PublicCloudConf struct {
	Enable bool `yaml:"enable"`
}
type IndexModuleConf struct {
	Enable       bool   `yaml:"enable"`
	ResourceName string `yaml:"resource_name"`
	Modulus      int    `yaml:"modulus"`
	Num          int    `yaml:"Num"`
}

type MySQLConf struct {
	Name  string `yaml:"name"`
	Addr  string `yaml:"addr"`
	Max   int    `yaml:"max"`
	Idle  int    `yaml:"idle"`
	Debug bool   `yaml:"debug"`
}

//根据io read 读取配置文件的字符串解析成yaml

func Load(s []byte) (*Config, error) {
	cfg := &Config{}
	err := yaml.Unmarshal(s, cfg)
	if err != nil {
		return nil, err
	}
	return cfg, nil
}

// 根据conf 路径读取内容
func LoadFile(filename string) (*Config, error) {
	content, err := os.ReadFile(filename)
	if err != nil {
		return nil, err
	}
	cfg, err := Load(content)
	if err != nil {
		fmt.Printf("error parsing config file: %s", err)
		return nil, err
	}
	return cfg, nil
}

//基本的顺序，定义一个结构体，之后为了提高兼容性定义切片。读取配置文件--》[]byte--->调用Unmarshal
